package com.health.service.Impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.health.context.BaseContext;
import com.health.entity.*;
import com.health.mapper.UsersAccountMapper;
import com.health.mapper.UsersTradeMapper;
import com.health.result.PageResult;
import com.health.service.UsersTradeService;
import com.health.vo.OrderVO;
import com.health.vo.TradeAndAccountVO;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class UsersTradeServiceImpl implements UsersTradeService {

    @Autowired
    private UsersTradeMapper usersTradeMapper;

    @Autowired
    private UsersAccountMapper usersAccountMapper;

    @Override
    public List<TradeAndAccountVO> list(Integer page, Integer pageSize) {
        //UserId
        Long userId = BaseContext.getCurrentId();
        //Long userId = 10L;

        PageHelper.startPage(page,pageSize);
        //Page<TradeAndAccountVO> voPage = (Page<TradeAndAccountVO>) usersTradeMapper.list(userId);
        List<TradeAndAccountVO> result = usersTradeMapper.list(userId);

        List<UsersAccount> accounts = usersAccountMapper.getAll();
        Map<Long, Integer> accountsList = accounts.stream().collect(Collectors.toMap(UsersAccount::getUserId, UsersAccount::getBalance));

        //查询所有修改时间
        List<UsersTrade> usersTradeAll = usersTradeMapper.getUsersTradeAll(userId);
        Map<String, LocalDateTime> localDateTimeMap = usersTradeAll.stream().collect(Collectors.toMap(UsersTrade::getId, UsersTrade::getCreateTime));

       //List<TradeAndAccountVO> result = voPage.getResult();
        for (TradeAndAccountVO vo : result) {
            vo.setBalance(accountsList.get(vo.getUserId()));
            vo.setTypeValue("余额充值");
            //获取时间
            LocalDateTime t = localDateTimeMap.get(vo.getId());

            vo.setTime(t);
            String[] time = new String[6];
            time[1] = t.getMonthValue() + "";
            time[0] = t.getYear() + "";
            time[2] = t.getDayOfMonth() + "";
            time[3] = t.getHour() + "";
            time[4] = t.getMinute() + "";
            time[5] = t.getSecond() + "";
            vo.setCreateTime(time);
        }

        return result;

    }
}
